<?php

namespace Home\Model;

class MsgModel extends CommonModel {

    public function createMsg($outer_id, $type){
        $dt = array();
        switch ($type) {
            case 'match':
                $dt = $this->createMsgMatch($outer_id, $type);
            default:
                // break;
        }
        return $this->addMsg($outer_id, $type, $dt);
    }

    public function addMsg($outer_id, $type, $dt){
        if (empty($dt)){
            $ret['status'] = false;
            $ret['msg'] = $this->getError();
        }
        $tm = time();
        $dt['team_id'] = $_REQUEST['home_team_id'];
        $dt['outer_id'] = $outer_id;
        $dt['type'] = $type;
        $dt['create_time'] = $tm;
        $dt['update_time'] = $tm;

        $rs = $this->add($dt);
        $ret = array();
        if ($rs) {
            $ret['status'] = true;
            $ret['msg'] = '操作成功！';
        } else {
            $ret['status'] = false;
            $ret['msg'] = $this->getError();
        }
        return $ret;
    }

    public function getContent($outer_id, $type){
        switch ($type) {
            case 'match':
                return $this->contentMatch($outer_id, $type);
                break;
            default:
                break;
        }
    }

    public function getBtns($outer_id, $type){
        switch ($type) {
            case 'match':
                return $this->btnsMatch($outer_id, $type);
                break;
            default:
                break;
        }
    }


    public function createMsgMatch($outer_id, $type){
        $content = $this->getContent($outer_id, $type);
        $btns = $this->getBtns($outer_id, $type);

        $dt = array(
            'content' => $content,
            'btns' => $btns,
        );
        return $dt;
    }

    public function contentMatch($outer_id, $type){
        $ret = "";
        $this->mdlMatch = D('match');
        $match = $this->mdlMatch->findMatchByMatchId(array('match_id'=>$outer_id));

$ret = <<<EOT
比赛:{$match['name']}<br/>
时间:{$match['match_time']}<br/>
场地:{$match['court_name']}<br/>
EOT;

        return $ret;
    }

    public function btnsMatch($outer_id, $type){
        $ret = "";

$ret = <<<EOT

<span style="margin-right: 20px;">
<a href="./index.php?m=Home&c=Match&a=join&match_id={$outer_id}&team_id={$_REQUEST['home_team_id']}"><span>参加</span></a>
</span>
EOT;

        return $ret;
    }

    public function findLast($teamId){
        $flt = array(
            'team_id' => $teamId,
            'is_del' => 0,
        );
        return $this->where($flt)->order('create_time desc')->find();
    }

}
